package com.xingkeduo;

import com.gexin.fastjson.JSON;
import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import com.xingkeduo.dto.WebsocketMessageResponse;

public class AppPushTest {

    //采用"Java SDK 快速入门"， "第二步 获取访问凭证 "中获得的应用配置，用户可以自行替换
    private static String appId = "zfi3wuQaB491C8qzgCBuM9";
    private static String appKey = "QePlO0qKQA66IxxTg9m6y3";
    private static String masterSecret = "0aUlkLuUoR9LOdYusWFEB3";
    private static String host = "http://sdk.open.api.igexin.com/apiex.htm";

    static String CID = "021ed1a994b0070b9a938c42a9c0ff68";
    //别名推送方式
     static String Alias = "11111";

    private static IGtPush push = new IGtPush(host, appKey, masterSecret);




    public static void main(String[] args) throws Exception {
        TransmissionTemplate template = linkTemplateDemo();
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        //target.setClientId(CID);
        target.setAlias(Alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
        } else {
            System.out.println("服务器响应异常");
        }

        //unbindAlias();

        //bindAlias();
    }






    public static TransmissionTemplate linkTemplateDemo() {
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(2);
        template.setTransmissionContent("请输入需要透传的内容11111111111111111");
        return template;
    }


    public static void bindAlias() {
        IAliasResult iAliasResult = push.bindAlias(appId, "11111", "021ed1a994b0070b9a938c42a9c0ff68");
        System.out.println(JSON.toJSON(iAliasResult));
    }

    public static void unbindAlias() {
        IAliasResult iAliasResult = push.unBindAlias(appId, "11111", "021ed1a994b0070b9a938c42a9c0ff68");
        System.out.println(JSON.toJSON(iAliasResult));
    }




}
